# Contributor: Jeff Bilyk <jbilyk@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=smokeping
pkgver=2.7.3
pkgrel=7
pkgdesc="Smokeping network latency monitoring"
pkgusers="smokeping"
pkggroups="smokeping"
install="$pkgname.pre-install"
url="https://oss.oetiker.ch/smokeping/"
options="!check"  # No test suite
source="https://oss.oetiker.ch/smokeping/pub/smokeping-$pkgver.tar.gz
	smokeping.initd
	musl-lc_numeric-utf8.patch
	"
depends="
	fping
	perl
	perl-cgi
	perl-cgi-fast
	perl-cgi-session
	perl-config-grammar
	perl-data-hexdump
	perl-digest-hmac
	perl-fcgi
	perl-io-socket-ssl
	perl-io-tty
	perl-ldap
	perl-libwww
	perl-mozilla-ca
	perl-net-dns
	perl-net-ip
	perl-net-openssh
	perl-net-snmp
	perl-net-telnet
	perl-text-soundex
	perl-rrd
	perl-snmp-session
	perl-uri
	rrdtool
	ttf-dejavu
	"
makedepends="
	openssl1.1-compat-dev
	perl-dev
	perl-try-tiny
	zlib-dev
	"
arch="noarch !riscv64"
license="GPL-2.0-or-later"
subpackages="$pkgname-doc"

_vendorlib=/usr/share/perl5/vendor_perl

prepare() {
	default_prepare

	# provided by perl-snmp-session
	rm -r lib/BER.pm lib/SNMP_Session.pm lib/SNMP_util.pm
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--bindir=/usr/bin \
		--libdir=/usr/lib \
		--sysconfdir=/etc/smokeping \
		--libexecdir=/usr/libexec \
		--datarootdir=/usr/share \
		--mandir=/usr/share/man \
		--with-htdocs-dir=/usr/share/webapps/smokeping

	make DESTDIR="$pkgdir"
}

package() {
	make DESTDIR="$pkgdir" install

	for i in basepage.html config smokemail smokeping_secrets tmail; do
		mv "$pkgdir"/etc/smokeping/$i.dist "$pkgdir"/etc/smokeping/$i
	done

	# Next part here cleans up the mess that the Makefile leaves behind...

	mkdir -p "$pkgdir"/$_vendorlib
	mv "$pkgdir"/usr/lib/Smokeping.pm "$pkgdir"/$_vendorlib/
	mv "$pkgdir"/usr/lib/Smokeping "$pkgdir"/$_vendorlib/

	# Fix path to config file
	sed -i -e 's#/usr/etc/config#/etc/smokeping/config#' "$pkgdir"/usr/share/webapps/smokeping/smokeping.fcgi.dist

	# Install cgi in the right spot and fix permissions
	mv "$pkgdir"/usr/share/webapps/smokeping/smokeping.fcgi.dist "$pkgdir"/usr/share/webapps/smokeping/smokeping.cgi
	chmod 755 "$pkgdir"/usr/share/webapps/smokeping/smokeping.cgi

	# Fix up some paths within smokeping.cgi
	sed -i -e 's:^use lib:#use lib:' \
		-e 's:/etc/config:/etc/smokeping/config:' \
		-e '1s:.*:#!/usr/bin/perl:' \
		"$pkgdir"/usr/bin/smokeping_cgi

	for i in smokeping smokeinfo; do
		sed -e 's:^use lib:#use lib:g' \
			-e 's:etc/config:etc/smokeping/config:' \
			-i "$pkgdir"/usr/bin/$i
	done

	sed -e 's#Smokeping::main("$FindBin::Bin/../etc/config")#Smokeping::main("/etc/smokeping/config")#' \
		-i "$pkgdir"/usr/bin/smokeping

	sed -e 's:etc/config.dist:etc/smokeping/config:' \
		-i "$pkgdir"/usr/bin/tSmoke

	# config
	sed -e '/^imgcache/{s:\(^imgcache[ \t]*=\).*:\1 /var/lib/smokeping/.simg:}' \
		-e '/^imgurl/{s:\(^imgurl[ \t]*=\).*:\1 img:}' \
		-e '/^datadir/{s:\(^datadir[ \t]*=\).*:\1 /var/lib/smokeping:}' \
		-e '/^piddir/{s:\(^piddir[ \t]*=\).*:\1 /var/run/smokeping:}' \
		-e '/^cgiurl/{s#\(^cgiurl[ \t]*=\).*#\1 http://some.place.xyz/perl/smokeping.pl#}' \
		-e '/^smokemail/{s:\(^smokemail[ \t]*=\).*:\1 /etc/smokeping/smokemail:}' \
		-e '/^tmail/{s:\(^tmail[ \t]*=\).*:\1 /etc/smokeping/tmail:}' \
		-e '/^secrets/{s:\(^secrets[ \t]*=\).*:\1 /etc/smokeping/smokeping_secrets:}' \
		-e '/^template/{s:\(^template[ \t]*=\).*:\1 /etc/smokeping/basepage.html:}' \
		-e '/^sendmail/{s:usr/lib:usr/sbin:}' \
		-i "$pkgdir"/etc/smokeping/config

	# Create softlink for image directory
	ln -s /var/lib/smokeping/.simg/ "$pkgdir"/usr/share/webapps/smokeping/img

	sed -e '/^<script/{s:cropper/:/cropper/:}' \
		-i "$pkgdir"/etc/smokeping/basepage.html

	chmod 700 "$pkgdir"/etc/smokeping/smokeping_secrets

	# initscript
	install -Dm755 "$srcdir"/smokeping.initd "$pkgdir"/etc/init.d/smokeping

	install -d -o smokeping -g smokeping -m755 \
		"$pkgdir"/var/lib/smokeping \
		"$pkgdir"/var/lib/smokeping/.simg \
		"$pkgdir"/var/run/smokeping
}

sha512sums="d46d61539ffdd9cac41391aa813364260f90f84443c328b1c9233ab6ba3a0c157861694614e6c33e18b153f6a6afc0769c5fc7752b3ab52f114d984e53232830  smokeping-2.7.3.tar.gz
58189cb39bb085022b0c1197ccc59e06412d1cfb62bace79a168a8c63952c1ccc051cddb48c1d9260a2b1246306d90e5c08323e35c21de66fa663e3f98325e02  smokeping.initd
e6bc389d8edbc000f5ad65848308197b3eb1ddb24bfa9df9c05fa027103952484383b7e021bea86795dceccdcf3f3d51a24c22cae3a68dc5afa30a72b2dbf803  musl-lc_numeric-utf8.patch"
